package com.gl.emms.whale.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.constant.EMMSConstant;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.nio.mutual.RequestBody;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.util.RoomViewEntity;
import com.gl.emms.whale.util.SharePreferenceUtil;
import com.gl.emms.whale.util.UpdateManager;

public class LoginActivity extends WhaleMonitorActivity implements
OnClickListener {
	private UpdateManager mUpdateManager;//更新提示
	
	
	
	private EditText accountEdit,pwdEdit;
	private Button mBtnLogin;
	private Button mBtnRegister;
	CheckBox rememberPassword ;
	
	private View mMoreView;// “更多登录选项”的view
	private boolean mShowMenu = false;// “更多登录选项”的内容是否显示
	private View mMoreMenuView;// “更多登录选项”中的内容view
	private ImageView mMoreImage;// “更多登录选项”的箭头图片
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initViews();
		checkUpdate();
	}

	private void initViews() {

		mMoreView = findViewById(R.id.more);
		mMoreMenuView = findViewById(R.id.moremenu);
		mMoreImage = (ImageView) findViewById(R.id.more_image);
		mMoreView.setOnClickListener(this);
		
		
		accountEdit = (EditText)findViewById(R.id.lgoin_accounts);
		pwdEdit = (EditText)findViewById(R.id.login_password);
		mBtnRegister = (Button) findViewById(R.id.regist_btn);
		mBtnRegister.setOnClickListener(this);

		mBtnLogin = (Button) findViewById(R.id.login_btn);
		mBtnLogin.setOnClickListener(this);

		SharePreferenceUtil util = new SharePreferenceUtil(this, Constant.SAVE_USERINFO);
		accountEdit.setText(util.getUserName());
		rememberPassword = (CheckBox)findViewById(R.id.auto_save_password);
		if (rememberPassword.isChecked()) {
			pwdEdit.setText(util.getPassword());
		}

	}

	private void doLogin() {
		SharePreferenceUtil util = new SharePreferenceUtil(this, Constant.SAVE_USERINFO);
		util.setUserName(accountEdit.getText().toString());
		if (rememberPassword.isChecked()) {
			util.setPassword(pwdEdit.getText().toString());
		}else
		{
			util.setPassword("");
		}

		if ((!"".equals(accountEdit.getText().toString().trim()))&&(!"".equals(pwdEdit.getText().toString().trim()))) {
			showProgressDialog("登陆提示", "正在登陆，请稍后......");
			EMMSPushManager.setAccount(this,MyApplication.getUserData().getId(),accountEdit.getText().toString(),pwdEdit.getText().toString());
			
		}else
		{
			showToask("用户名或密码不能为空");
		}


	}
	private void doregist() {

		Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);  
		startActivity(intent);

	}
	@Override
	public void onReplyReceived(final ReplyBody reply) {
		super.onReplyReceived(reply);
		if (reply.getType().equals(Constant.TYPE_REGISTER_LOGIN)) {
			hideProgressDialog();
			if(reply.get(Constant.CODE_LOGIN).equals(Constant.LOGIN_SUCCESS))
			{
				
				String roomsString = "";
				MyApplication.getUserData().setIsOnline(true);
				MyApplication.getUserData().setName(reply.get("username"));
				MyApplication.getUserData().setPhone(reply.get("phone"));
				MyApplication.getUserData().setMainroom(reply.get("mainroom"));
				MyApplication.getUserData().setElseroom(reply.get("elseeoom"));
				if (Integer.parseInt(reply.get("roomnum")) == 1) {
					roomsString = reply.get("mainroom");
				}else {
					roomsString = reply.get("mainroom")+":"+reply.get("elseeoom");
				}
				
				RoomViewEntity roomViewEntity = new RoomViewEntity(roomsString,reply.get("ye"), reply.get("bjsz"), reply.get("fmzt"), reply.get("zje"), reply.get("jswd"), reply.get("hswd"), reply.get("ls"), reply.get("time"),reply.get("dbye"),reply.get("sbyl"));
				// 隐藏输入法
				InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
				// 显示或者隐藏输入法
				imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				showToask("登陆成功");
				
				Intent intent = new Intent(LoginActivity.this,TabActivity.class);  
				intent.putExtra("RoomView", roomViewEntity);  
				startActivity(intent);
				finish();
			}else
			{
				String result = "";
				if(reply.get(Constant.CODE_LOGIN).equals(Constant.LOGIN_FAIL))
				{
					result = getString(R.string.login_fail_nameorpasswordwrong);
				}else if(reply.get(Constant.CODE_LOGIN).equals(Constant.LOGIN_USERLOGINREPEAT))
				{
					result = getString(R.string.login_fail_userloginrepeat);
				}else
				{
					result = getString(R.string.login_fail_unknow);
					result += reply.get("login_code");
				}
				showToask(getString(R.string.login_fail) + result);
			}
			
		}

	}





	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.login_btn:
			doLogin();
			break;
		case R.id.regist_btn:
			doregist();
			break;
		case R.id.more:
			showMoreView(!mShowMenu);
			break;
		}

	}

	@Override
	public void onBackPressed() {

		EMMSPushManager.destory(this);
		this.finish();
	}

	public void showMoreView(boolean bShow) {
		if (bShow) {
			mMoreMenuView.setVisibility(View.GONE);
			mMoreImage.setImageResource(R.drawable.login_more_up);
			mShowMenu = true;
		} else {
			mMoreMenuView.setVisibility(View.VISIBLE);
			mMoreImage.setImageResource(R.drawable.login_more);
			mShowMenu = false;
		}
	}
	public void checkUpdate() {
		String curruntVersion = getVersion();
		showToask( "lastVersion:"+MyApplication.getUserData().getLastversion());
		if(Float.valueOf(MyApplication.getUserData().getLastversion())>Float.valueOf(curruntVersion))
		{
			showToask( "curruntVersion:"+curruntVersion);
			mUpdateManager = new UpdateManager(this);
			mUpdateManager.checkUpdateInfo();
		}else
		{
			showToask( "当前为最新版本");
		}
		
		
	}

}
